package sy.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import sy.jackson.AppPlantMixin;
import sy.util.MessageObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.model.AppPlant;
import sy.model.SysProject;
import sy.service.AppPlantServiceI;
import sy.service.SysProjectServiceI;
/**
 * @Description: 首页
 * @param 
 * @author     :wjy
 * Create Date : 2017年7月26日 
 */
@Controller
@SessionAttributes("currAccount")
@RequestMapping("plantController")
public class PlantController {
	
	@Autowired
	private AppPlantServiceI appPlantService;
	@Autowired
	private SysProjectServiceI SysProjectService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();

	public static ObjectMapper getObjectmapper() {
		return objectMapper;
	}

	/************************** view controller ***************************/

	/**
	 * @Description: 首页视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年8月26日 
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<AppPlant> appPlantList = appPlantService.getList();
		
		model.addAttribute("appPlantList",appPlantList);
		return "baseInfo/plant/plant";
	}
	
	/**
	 * @Description: 新增视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年8月26日 
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		AppPlant appPlant = new AppPlant();
		appPlant.setId(uuid);
		
		List<SysProject> sysProjectList = SysProjectService.list();

		model.addAttribute("appPlant",appPlant);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",1);
		
		return "baseInfo/plant/plantFrom";
	}

	/**
	 * @Description: 编辑视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年8月26日 
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		AppPlant appPlant = appPlantService.getAppPlantById(id);
		List<SysProject> sysProjectList = SysProjectService.list();

		model.addAttribute("appPlant",appPlant);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",2);
		
		return "baseInfo/plant/plantFrom";
	}
	
	/************************** data controller ***************************/
	
	/**
	 * @Description: 增加
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo( @RequestBody AppPlant appPlant, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			appPlantService.save(appPlant);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 编辑
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月28日 
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody AppPlant appPlant, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			appPlantService.update(appPlant);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 删除
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String[] ids) {
		MessageObject messageObject = new MessageObject();
		
		try {
			appPlantService.remove(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
	
	/**
	 * @Description: 返回全部json数据
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		objectMapper.addMixInAnnotations(AppPlant.class, AppPlantMixin.BasicInfo.class);
		List<AppPlant> appPlantList = appPlantService.getList();
		try {
			return objectMapper.writeValueAsString(appPlantList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
}
